package org.red5.demo;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 采用安装ffmpeg 后配置环境变量 用java 执行启动ffmpeg 命令方式启动视频流 比javacv启动稳定丢包率低
 */
public class ThumbnailTool {


    public static  void testFFmpegRtsp(String INPUT_PATH,String OUT_PATH,int width,int height) {
        ProcessBuilder processBuilder = new ProcessBuilder();
        //定义命令内容
        // ffmpeg -i "rtsp://admin:12345@192.168.20.15:554/h264/ch1/main/av_stream"
         //   -f flv -r 25 -s 640x480 -an "rtmp://localhost/oflaDemo/192.168.20.15"
        List<String> command = new ArrayList<>();
        command.add("ffmpeg");
        command.add("-i");
        command.add(INPUT_PATH);
        command.add("-f");//覆盖输出文件
        command.add("flv");
        command.add("-r");
        command.add("25");
        command.add("-s");
        command.add(width+"x"+height);
        command.add("-an");
        //command.add("rtmps://localhost:8443/oflaDemo/192.168.20.13");
        command.add(OUT_PATH);
        processBuilder.command(command);
        //将标准输入流和错误输入流合并，通过标准输入流读取信息
        processBuilder.redirectErrorStream(true);
        try {
            //启动进程
            System.out.println(OUT_PATH);
            Process start = processBuilder.start();
            //获取输入流
            InputStream inputStream = start.getInputStream();
            //转成字符输入流
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
            int len = -1;
            char[] c = new char[1024];
            StringBuffer outputString = new StringBuffer();
            //读取进程输入流中的内容
            while ((len = inputStreamReader.read(c)) != -1) {
                String s = new String(c, 0, len);
                outputString.append(s);
                System.out.print(s);
            }
            inputStream.close();
        } catch (Exception  e) {
            e.printStackTrace();
        }
    }



}